package com.zs.myorm.framework.core.factory.impl;

import java.util.List;
import java.util.Properties;

import javax.sql.DataSource;

import com.zs.myorm.framework.core.datasource.JdbcDataSource;
import com.zs.myorm.framework.core.execute.ISqlExecute;
import com.zs.myorm.framework.core.execute.ISqlExecuteRegister;
import com.zs.myorm.framework.core.execute.impl.DefaultSqlExecuteImpl;
import com.zs.myorm.framework.core.factory.AbstractSqlSessionFactory;
import com.zs.myorm.framework.core.factory.ISqlSessionFactory;
import com.zs.myorm.framework.core.method.IMethodHandler;
import com.zs.myorm.framework.core.method.IMethodHandlerRegister;
import com.zs.myorm.framework.core.session.ISqlSession;
import com.zs.myorm.framework.core.session.impl.DefaultSqlSession;

public class JdbcSqlSessionFactory extends AbstractSqlSessionFactory implements ISqlSessionFactory, ISqlExecuteRegister,IMethodHandlerRegister {

	private Properties config;
	private DataSource dataSource;
	private ISqlExecute sqlExecute;
	private List<IMethodHandler> methodHandlers;
	
	public JdbcSqlSessionFactory() {
	}

	public JdbcSqlSessionFactory(Properties config) {
		this.config = config;
		updata();
		initDataSource();
	}
	private void initDataSource() {
		this.dataSource = new JdbcDataSource(config);
		registerSqlExecute(new DefaultSqlExecuteImpl(dataSource));
	}

	public ISqlSession getSession() {
		DefaultSqlSession session = new DefaultSqlSession();
		session.setSqlExecute(sqlExecute);
		return session;
	}

	public void registerSqlExecute(ISqlExecute execute) {
		this.sqlExecute = execute;
	}

	@Override
	public void registerMethodHandler(IMethodHandler handler) {
		methodHandlers.add(handler);
	}

}
